package cn.lil.model;

import java.time.LocalDateTime;
import java.util.*;

public class Order {
    private int id;
    private float total;
    private int amount;
    private int status;
    private int payType;
    private int userId;
    private LocalDateTime datetime;



    private Map<Integer, OrderItem> itemMap = new HashMap<>();
    private List<OrderItem> itemList = new ArrayList<>();

    public List<OrderItem> getItemList() {
        return itemList;
    }

    public void setItemList(List<OrderItem> itemList) {
        this.itemList = itemList;
    }
    public void addBearing(Bearing b) {
        if (itemMap.containsKey(b.getId())) {
            OrderItem item = itemMap.get(b.getId());
            item.setAmount(item.getAmount() + 1);
        } else {
            OrderItem item = new OrderItem(0, b.getPrice(), 1, b.getId(), this.id);
        }
        amount++;
        total = total + b.getPrice();
    }

    public void lessen(int bearingId) {
        if (itemMap.containsKey(bearingId)) {
            OrderItem item = itemMap.get(bearingId);
            item.setAmount(item.getAmount() - 1);
            amount--;
            total = total - item.getPrice();
            if (item.getAmount() <= 0) {
                itemMap.remove(bearingId);
            }
        }
    }

    public void delete(int bearingId) {
        if (itemMap.containsKey(bearingId)) {
            OrderItem item = itemMap.get(bearingId);
            total = total - (item.getAmount() * item.getPrice());
            amount -= item.getAmount();
            itemMap.remove(bearingId);
        }
    }

    public Order() {
    }

    public Order(int id, float total, int amount, int status, int payType, int userId, LocalDateTime datetime) {
        this.id = id;
        this.total = total;
        this.amount = amount;
        this.status = status;
        this.payType = payType;
        this.userId = userId;
        this.datetime = datetime;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public float getTotal() {
        return total;
    }

    public void setTotal(float total) {
        this.total = total;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public LocalDateTime getDatetime() {
        return datetime;
    }

    public void setDatetime(LocalDateTime datetime) {
        this.datetime = datetime;
    }

    public Map<Integer, OrderItem> getItemMap() {
        return itemMap;
    }

    public void setItemMap(Map<Integer, OrderItem> itemMap) {
        this.itemMap = itemMap;
    }

    @Override
    public String toString() {
        return "Order{" +
                "id=" + id +
                ", total=" + total +
                ", amount=" + amount +
                ", status=" + status +
                ", payType=" + payType +
                ", userId=" + userId +
                ", datetime=" + datetime +
                ", itemMap=" + itemMap +
                ", itemList=" + itemList +
                '}';
    }
}

